package com.platform.controller;

import com.platform.Global;
import com.platform.entity.StationEntity;
import com.platform.entity.SysUserEntity;
import com.platform.service.StationService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author zhangzehao
 * @email 862583291@qq.com
 * @date 2018-08-1 15:02:28
 */
@RestController
@RequestMapping("station")
public class StationController {
    @Autowired
    private StationService stationService;
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("station:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<StationEntity> stationList = stationService.queryList(query);
        int total = stationService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(stationList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("station:save")
    public R save(@RequestBody StationEntity station) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession(true);
        SysUserEntity user =  (SysUserEntity)session.getAttribute(Global.CURRENT_USER);
        station.setUserId(user.getUserId());
        stationService.save(station);

        return R.ok();
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("station:info")
    public R info(@PathVariable("id") Integer id) {
        StationEntity station = stationService.queryObject(id);

        return R.ok().put("station", station);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("station:update")
    public R update(@RequestBody StationEntity station) {
        stationService.update(station);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("station:delete")
    public R delete(@RequestBody Integer[] ids) {
        stationService.deleteBatch(ids);

        return R.ok();
    }
}
